#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "../PropertyData.h"

void toBinaryFile(const char *txtFile, const char *binFile, const char *fmt, char mode)
{
	//const char *txtFile = "ctyw.txt", *binFile = "ctyw.dat";
	FILE *pTxtFile, *pBinFile;
	pTxtFile = fopen(txtFile, "r");
	pBinFile = fopen(binFile, "wb");
	if (pTxtFile == NULL || pBinFile == NULL)
	{
		printf("open %s 或 %s failed\n", txtFile, binFile);
		return;
	}

	rewind(pTxtFile);
	//const char *fmt = "%lf %s %s %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %s\n";
	
	while(!feof(pTxtFile))
	{
		switch(mode){
			case 'C':
			case 'c':
				CTYW ctyw;
				fscanf(pTxtFile, fmt, 
						&ctyw.id,ctyw.province,ctyw.city,&ctyw.ta_year,&ctyw.ta_day_5,&ctyw.ta_day_8,
			            &ctyw.ta_month_h,&ctyw.ta_ac,&ctyw.phi,&ctyw.w_winter_m,&ctyw.w_winter,
			            &ctyw.w_summer,&ctyw.ta_lowest,&ctyw.ta_highest,ctyw.styear);
	            fwrite(&ctyw, sizeof(ctyw), 1, pBinFile);
	            break;
	        case 'I':
	        case 'i':
	        	Insul insul;
	        	fscanf(pTxtFile, fmt, 
						&insul.id,insul.name,insul.remark,&insul.density_min,&insul.density_max,&insul.allowTemperature_min,
		            	&insul.allowTemperature_max,&insul.reTemperature_min,&insul.reTemperature_max,&insul.tc0,
		            	&insul.tm_min,&insul.tm_max,&insul.a,&insul.b,&insul.c,&insul.d,&insul.e,&insul.f,&insul.g,
		            	&insul.strength,&insul.strength_196);
	            fwrite(&insul, sizeof(insul), 1, pBinFile);
	            break;
	        case 'M':
	        case 'm':
	        	PipeMaterialProperties pmp;
	        	fscanf(pTxtFile, "%u %s %s %s %s %s %s %s %s %s %u %u %s %u %u %u", 
						&pmp.id,pmp.stressCode,pmp.ShapeCode,pmp.MClass,pmp.ClassCode,
            			pmp.pipeName,pmp.weldingCode,pmp.pipeCode,pmp.MCode,pmp.process,
            			&pmp.thickness_min,&pmp.thickness_max,pmp.temperature_min,&pmp.rm,
            			&pmp.rel,&pmp.temperature_max);
	        	for (size_t i = 0; i < 36; i++)
		        {
		            fscanf(pTxtFile, " %lf", &pmp.stress[i]);
		        }
		        fscanf(pTxtFile, " %lf %s", &pmp.stress_tmax, pmp.remark);
	            fwrite(&pmp, sizeof(pmp), 1, pBinFile);
	            break;    
			default:
				printf("%s\n", "mode 'c':for CTYW, 'i':for Insul, 'm' for PipeMaterialProperties");
		}
		
	}

	fclose(pTxtFile);
	fclose(pBinFile);
}

int main(int argc, char const *argv[])
{
	for (int i = 0; i < argc; ++i)
	{
		if (!strcmp("C", argv[i]))
		{
			const char *fmt = "%lf %s %s %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %s\n";
			const char *txtFile = "ctyw.txt", *binFile = "ctyw.dat";
			toBinaryFile(txtFile, binFile, fmt, 'c');
		}
		if (!strcmp("I", argv[i]))
		{
			const char *fmt = "%u %s %s %u %u %d %d %d %d %lf %d %d %lf %lf %lf %lf %lf %lf %lf %lf %lf\n";
			const char *txtFile = "Insulation.txt", *binFile = "Insulation.dat";
			toBinaryFile(txtFile, binFile, fmt, 'i');
		}
		if (!strcmp("M", argv[i]))
		{
			const char *fmt = "";
			const char *txtFile = "Material.txt", *binFile = "Material.dat";
			toBinaryFile(txtFile, binFile, fmt, 'm');
		}
		if (!strcmp("RC", argv[i]))
		{
			const char *filePath = "ctyw.dat";
			CTYW *ctyw = (CTYW*)malloc(sizeof(CTYW));
			getPropertyData(filePath, ctyw, 0, 1);
			printf("%f %s\n", ctyw->id, ctyw->city);
		}
		if (!strcmp("RI", argv[i]))
		{
			const char *filePath = "Insulation.dat";
			Insul *insul = (Insul*)malloc(sizeof(Insul));
			getPropertyData(filePath, insul, 0, 1);
			printf("%u %s\n", insul->id, insul->name);
		}
		if (!strcmp("RM", argv[i]))
		{
			const char *filePath = "Material.dat";
			PipeMaterialProperties *pmp = (PipeMaterialProperties*)malloc(sizeof(PipeMaterialProperties));
			getPropertyData(filePath, pmp, 0, 1);
			printf("%u %s\n", pmp->id, pmp->pipeName);
		}
		if (!strcmp("RC-A", argv[i]))
		{
			const char *filePath = "ctyw.dat";
			CTYW *ctyw = (CTYW*)malloc(sizeof(CTYW) * 255);
			int row = getPropertyData(filePath, ctyw, 0, 0);
			for (int i = 0; i < row; ++i)
			{
				printf("%f %s\n", (ctyw ++)->id, ctyw->city);
			}
			free(ctyw);
		}
		if (!strcmp("RI-A", argv[i]))
		{
			const char *filePath = "Insulation.dat";
			Insul *insul = (Insul*)malloc(sizeof(Insul) * 255);
			int row = getPropertyData(filePath, insul, 0, 0);
			for (int i = 0; i < row; ++i)
			{
				printf("%u %s\n", (insul ++)->id, insul->name);
			}
			free(insul);
		}
		if (!strcmp("RM-A", argv[i]))
		{
			const char *filePath = "Material.dat";
			PipeMaterialProperties *pmp = (PipeMaterialProperties*)malloc(sizeof(PipeMaterialProperties) * 258);
			int row = getPropertyData(filePath, pmp, 0, 256);
			for (int i = 0; i < row; ++i)
			{
				printf("%u %s %s %s %s\n", (pmp ++)->id, pmp->pipeName, pmp->pipeCode, pmp->MCode, pmp->remark);
			}
			free(pmp);
		}
	}
	return 0;
}